package com.apex.sdk.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.WeatherCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.EABleWeather;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityWeatherBinding;
import com.apex.sdk.dialog.CurrentWeatherTemperatureDialog;
import com.apex.sdk.dialog.MoonDialog;
import com.apex.sdk.dialog.UvDialog;
import com.apex.sdk.dialog.WaitingDialog;
import com.apex.sdk.dialog.WeatherDialog;
import com.apex.sdk.dialog.WeatherTemperatureUnitDialog;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class WeatherActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    SimpleDateFormat simpleDateFormat;
    private float cTemp, iTemp, aTemp;
    String lastUnit;
    private WeatherTemperatureUnitDialog weatherTemperatureUnitDialog;
    private CurrentWeatherTemperatureDialog currentWeatherTemperatureDialog, minTempDialog, maxTempDialog;
    private WeatherDialog dayWeatherDialog, nightWeatherDialog;
    private MoonDialog moonDialog;
    private UvDialog uvDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                EABleWeather eaBleWeather = (EABleWeather) msg.obj;
                Log.e(TAG, "获取到的天气信息:" + JSONObject.toJSONString(eaBleWeather));
                EABleWeather.TemperatureUnit temperatureUnit = eaBleWeather.getTemperatureUnit();
                if (temperatureUnit != null) {
                    if (temperatureUnit == EABleWeather.TemperatureUnit.centigrade) {
                        activityWeatherBinding.temperatureUnit.setText(getString(R.string.temperature_unit_centigrade));
                        lastUnit = getString(R.string.temperature_unit_centigrade);
                    } else {
                        activityWeatherBinding.temperatureUnit.setText(getString(R.string.temperature_unit_fahrenheit));
                        lastUnit = getString(R.string.temperature_unit_fahrenheit);
                    }
                }
                activityWeatherBinding.currentTemperature.setText(eaBleWeather.getCurrent_temperature() + "");
                cTemp = eaBleWeather.getCurrent_temperature();
                if (!TextUtils.isEmpty(eaBleWeather.getPlace()) && "".equalsIgnoreCase(eaBleWeather.getPlace())) {
                    activityWeatherBinding.place.setText(eaBleWeather.getPlace());
                }

                if (eaBleWeather.getS_day() == null || eaBleWeather.getS_day().isEmpty()) {
                    Toast.makeText(WeatherActivity.this, getString(R.string.no_weather), Toast.LENGTH_SHORT).show();
                } else {
                    EABleWeather.EABleWeatherItem item = eaBleWeather.getS_day().get(0);
                    activityWeatherBinding.daytimeWeather.setText(item.getE_day_type() != null ? item.getE_day_type().toString() : getString(R.string.weather_type_clear));
                    activityWeatherBinding.nightWeather.setText(item.getE_night_type() != null ? item.getE_night_type().toString() : getString(R.string.weather_type_clear));
                    iTemp = item.getMin_temperature();
                    activityWeatherBinding.minTemperature.setText(item.getMin_temperature() + "");
                    aTemp = item.getMax_temperature();
                    activityWeatherBinding.maxTemperature.setText(item.getMax_temperature() + "");
                    Calendar calendar = Calendar.getInstance();
                    if (item.getSunrise_timestamp() > 0) {
                        calendar.setTimeInMillis(item.getSunrise_timestamp() * 1000);
                        activityWeatherBinding.sunriseTime.setText(simpleDateFormat.format(calendar.getTime()));
                    }
                    if (item.getSunset_timestamp() > 0) {
                        calendar.clear();
                        calendar.setTimeInMillis(item.getSunset_timestamp() * 1000);
                        activityWeatherBinding.sunsetTime.setText(simpleDateFormat.format(calendar.getTime()));
                    }
                    Log.e(TAG, "天气质量:" + item.getE_air().toString());
                    activityWeatherBinding.airQuality.setText(item.getE_air() != null ? item.getE_air().toString() : getString(R.string.weather_air_good));
                    activityWeatherBinding.minWindForce.setText(item.getMin_wind_power() + "");
                    activityWeatherBinding.maxWindForce.setText(item.getMax_wind_power() + "");
                    activityWeatherBinding.uvIntensity.setText(item.getE_rays() != null ? item.getE_rays().toString() : getString(R.string.weather_rays_weak));
                    activityWeatherBinding.airHumidity.setText(item.getAir_humidity() + "");
                    activityWeatherBinding.moon.setText(item.getE_moon() != null ? item.getE_moon().toString() : getString(R.string.weather_moon_new_moon));
                    activityWeatherBinding.cloudiness.setText(item.getCloudiness() + "");
                    activityWeatherBinding.airFraction.setText(item.getAir_grade() + "");

                }

            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(WeatherActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(WeatherActivity.this, getString(R.string.add_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityWeatherBinding activityWeatherBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityWeatherBinding = DataBindingUtil.setContentView(this, R.layout.activity_weather);
        activityWeatherBinding.setLifecycleOwner(this);
        setContentView(activityWeatherBinding.getRoot());
        activityWeatherBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityWeatherBinding.tool);
        getSupportActionBar().setTitle(null);
        activityWeatherBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lastUnit = getString(R.string.temperature_unit_centigrade);
        cTemp = 10;
        iTemp = 5;
        aTemp = 20;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 6);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        activityWeatherBinding.sunriseTime.setText(simpleDateFormat.format(calendar.getTime()));
        calendar.add(Calendar.HOUR_OF_DAY, 12);
        activityWeatherBinding.sunsetTime.setText(simpleDateFormat.format(calendar.getTime()));
        EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();

        if (state == EABleConnectState.STATE_CONNECTED) {
            if (waitingDialog == null) {
                waitingDialog = new WaitingDialog(WeatherActivity.this);
            }
            waitingDialog.show();
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.weather, new WeatherCallback() {
                @Override
                public void weatherInfo(EABleWeather eaBleWeather) {
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x40;
                        message.obj = eaBleWeather;
                        mHandler.sendMessage(message);
                    }
                }

                @Override
                public void mutualFail(int errorCode) {
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(0x41);
                    }
                }
            });
        }
        activityWeatherBinding.temperatureUnit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (weatherTemperatureUnitDialog == null) {
                    weatherTemperatureUnitDialog = new WeatherTemperatureUnitDialog(WeatherActivity.this);
                    weatherTemperatureUnitDialog.setSelectListener(new WeatherTemperatureUnitDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityWeatherBinding.temperatureUnit.setText(sex);
                            if (lastUnit.equalsIgnoreCase(getString(R.string.temperature_unit_fahrenheit))) {//法式度转摄氏度
                                if (sex.equalsIgnoreCase(getString(R.string.temperature_unit_centigrade))) {
                                    cTemp = (float) ((cTemp - 32) / 1.8);
                                    aTemp = (float) ((aTemp - 32) / 1.8);
                                    iTemp = (float) ((iTemp - 32) / 1.8);
                                    activityWeatherBinding.currentTemperature.setText(cTemp + "");
                                    activityWeatherBinding.maxTemperature.setText(aTemp + "");
                                    activityWeatherBinding.minTemperature.setText(iTemp + "");
                                }
                            } else {
                                if (sex.equalsIgnoreCase(getString(R.string.temperature_unit_fahrenheit))) {//摄氏度转法式度
                                    cTemp = (float) (1.8 * cTemp + 32);
                                    aTemp = (float) (1.8 * aTemp + 32);
                                    iTemp = (float) (1.8 * iTemp + 32);
                                    activityWeatherBinding.currentTemperature.setText(cTemp + "");
                                    activityWeatherBinding.maxTemperature.setText(aTemp + "");
                                    activityWeatherBinding.minTemperature.setText(iTemp + "");
                                }
                            }
                            lastUnit = sex;

                        }
                    });
                }
                if (!weatherTemperatureUnitDialog.isShowing()) {
                    weatherTemperatureUnitDialog.show();

                }
            }
        });
        activityWeatherBinding.currentTemperature.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentWeatherTemperatureDialog == null) {
                    currentWeatherTemperatureDialog = new CurrentWeatherTemperatureDialog(WeatherActivity.this);
                    currentWeatherTemperatureDialog.setSelectListener(new CurrentWeatherTemperatureDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            if (lastUnit.equalsIgnoreCase(getString(R.string.temperature_unit_fahrenheit))) {
                                float temp = Float.parseFloat(sex);
                                cTemp = (float) (1.8 * temp + 32);
                                activityWeatherBinding.currentTemperature.setText(cTemp + "");
                            } else {
                                activityWeatherBinding.currentTemperature.setText(sex);
                                cTemp = Float.parseFloat(sex);
                            }


                        }
                    });
                }
                if (!currentWeatherTemperatureDialog.isShowing()) {
                    currentWeatherTemperatureDialog.show();
                }
            }
        });
        activityWeatherBinding.daytimeWeather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dayWeatherDialog == null) {
                    dayWeatherDialog = new WeatherDialog(WeatherActivity.this);
                    dayWeatherDialog.setSelectListener(new WeatherDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityWeatherBinding.daytimeWeather.setText(sex);
                        }
                    });
                }
                if (!dayWeatherDialog.isShowing()) {
                    dayWeatherDialog.show();
                }


            }
        });
        activityWeatherBinding.nightWeather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (nightWeatherDialog == null) {
                    nightWeatherDialog = new WeatherDialog(WeatherActivity.this);
                    nightWeatherDialog.setSelectListener(new WeatherDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityWeatherBinding.nightWeather.setText(sex);
                        }
                    });
                }
                if (!nightWeatherDialog.isShowing()) {
                    nightWeatherDialog.show();
                }
            }
        });
        activityWeatherBinding.minTemperature.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (minTempDialog == null) {
                    minTempDialog = new CurrentWeatherTemperatureDialog(WeatherActivity.this);
                    minTempDialog.setSelectListener(new CurrentWeatherTemperatureDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            if (lastUnit.equalsIgnoreCase(getString(R.string.temperature_unit_fahrenheit))) {
                                float temp = Float.parseFloat(sex);
                                iTemp = (float) (1.8 * temp + 32);
                                activityWeatherBinding.minTemperature.setText(iTemp + "");
                            } else {
                                activityWeatherBinding.minTemperature.setText(sex);
                                iTemp = Float.parseFloat(sex);
                            }
                        }
                    });
                }
                if (!minTempDialog.isShowing()) {
                    minTempDialog.show();
                }
            }
        });
        activityWeatherBinding.maxTemperature.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (maxTempDialog == null) {
                    maxTempDialog = new CurrentWeatherTemperatureDialog(WeatherActivity.this);
                    maxTempDialog.setSelectListener(new CurrentWeatherTemperatureDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            if (lastUnit.equalsIgnoreCase(getString(R.string.temperature_unit_fahrenheit))) {
                                float temp = Float.parseFloat(sex);
                                aTemp = (float) (1.8 * temp + 32);
                                activityWeatherBinding.maxTemperature.setText(aTemp + "");
                            } else {
                                activityWeatherBinding.maxTemperature.setText(sex);
                                aTemp = Float.parseFloat(sex);
                            }
                        }
                    });
                }
                if (!maxTempDialog.isShowing()) {
                    maxTempDialog.show();
                }
            }
        });
        activityWeatherBinding.moon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (moonDialog == null) {
                    moonDialog = new MoonDialog(WeatherActivity.this);
                    moonDialog.setSelectListener(new MoonDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityWeatherBinding.moon.setText(sex);
                        }
                    });
                }
                if (!moonDialog.isShowing()) {
                    moonDialog.show();
                }

            }
        });
        activityWeatherBinding.uvIntensity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (uvDialog == null) {
                    uvDialog = new UvDialog(WeatherActivity.this);
                    uvDialog.setSelectListener(new UvDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityWeatherBinding.uvIntensity.setText(sex);
                        }
                    });
                }
                if (!uvDialog.isShowing()) {
                    uvDialog.show();
                }
            }
        });
        activityWeatherBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    EABleWeather eaBleWeather = new EABleWeather();
                    Log.e(TAG, "地址:" + activityWeatherBinding.place.getText().toString());
                    eaBleWeather.setPlace(activityWeatherBinding.place.getText().toString());
                    eaBleWeather.setCurrent_temperature(cTemp);
                    if (lastUnit.equalsIgnoreCase(getString(R.string.temperature_unit_fahrenheit))) {
                        eaBleWeather.setTemperatureUnit(EABleWeather.TemperatureUnit.fahrenheit);
                    } else {
                        eaBleWeather.setTemperatureUnit(EABleWeather.TemperatureUnit.centigrade);
                    }
                    List<EABleWeather.EABleWeatherItem> weatherItemList = new ArrayList<>();
                    eaBleWeather.setS_day(weatherItemList);
                    EABleWeather.EABleWeatherItem eaBleWeatherItem = new EABleWeather.EABleWeatherItem();
                    eaBleWeatherItem.setAir_humidity(Integer.parseInt(activityWeatherBinding.airHumidity.getText().toString()));
                    eaBleWeatherItem.setCloudiness(Integer.parseInt(activityWeatherBinding.cloudiness.getText().toString()));
                    String quality = activityWeatherBinding.airQuality.getText().toString();
                    Log.e(TAG, "空气质量:" + quality);
                    if (!TextUtils.isEmpty(quality)) {
                        if (quality.equalsIgnoreCase(getString(R.string.weather_air_good))) {
                            eaBleWeatherItem.setE_air(EABleWeather.AirQuality.good);
                        } else if (quality.equalsIgnoreCase(getString(R.string.weather_air_bad))) {
                            eaBleWeatherItem.setE_air(EABleWeather.AirQuality.bad);
                        } else {
                            eaBleWeatherItem.setE_air(EABleWeather.AirQuality.excellent);
                        }
                    } else {
                        eaBleWeatherItem.setE_air(EABleWeather.AirQuality.good);
                    }
                    String dayWeather = activityWeatherBinding.daytimeWeather.getText().toString();
                    if (!TextUtils.isEmpty(dayWeather)) {
                        if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_haze))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.haze);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_fog))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.fog);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_dust))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.dust);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_sandstorm))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.sandstorm);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_heavy_snow))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.heavy_snow);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_moderate_snow))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.moderate_snow);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_light_snow))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.light_snow);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_heavy_sleet))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.sleet);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_heavy_rain))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.heavy_rain);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_thunderstorm))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.thunderstorm);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_moderate_rain))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.moderate_rain);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_drizzle))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.drizzle);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_gloomy))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.gloomy);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_cloudy))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.cloudy);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_clear))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.clear);
                        } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_type_typhoon))) {
                            eaBleWeatherItem.setE_day_type(EABleWeather.WeatherType.typhoon);
                        }
                    }
                    String nWeather = activityWeatherBinding.nightWeather.getText().toString();
                    Log.e(TAG, "晚间天气:" + nWeather);
                    if (!TextUtils.isEmpty(nWeather)) {
                        if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_haze))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.haze);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_fog))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.fog);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_dust))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.dust);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_sandstorm))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.sandstorm);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_heavy_snow))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.heavy_snow);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_moderate_snow))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.moderate_snow);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_light_snow))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.light_snow);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_heavy_sleet))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.sleet);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_heavy_rain))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.heavy_rain);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_thunderstorm))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.thunderstorm);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_moderate_rain))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.moderate_rain);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_drizzle))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.drizzle);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_gloomy))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.gloomy);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_cloudy))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.cloudy);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_clear))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.clear);
                        } else if (nWeather.equalsIgnoreCase(getString(R.string.weather_type_typhoon))) {
                            eaBleWeatherItem.setE_night_type(EABleWeather.WeatherType.typhoon);
                        }
                    }
                    String moon = activityWeatherBinding.moon.getText().toString();
                    Log.e(TAG, "月相:" + moon);
                    if (!TextUtils.isEmpty(moon)) {
                        if (moon.equalsIgnoreCase(getString(R.string.weather_moon_half_moon_1))) {
                            eaBleWeatherItem.setE_moon(EABleWeather.Moon.half_moon_1);
                        } else if (moon.equalsIgnoreCase(getString(R.string.weather_moon_full_moon))) {
                            eaBleWeatherItem.setE_moon(EABleWeather.Moon.full_moon);
                        } else if (moon.equalsIgnoreCase(getString(R.string.weather_moon_waning_gibbous_moon))) {
                            eaBleWeatherItem.setE_moon(EABleWeather.Moon.waning_gibbous_moon);
                        } else if (moon.equalsIgnoreCase(getString(R.string.weather_moon_new_moon))) {
                            eaBleWeatherItem.setE_moon(EABleWeather.Moon.new_moon);
                        } else if (moon.equalsIgnoreCase(getString(R.string.weather_moon_waning_crescent_moon))) {
                            eaBleWeatherItem.setE_moon(EABleWeather.Moon.waning_crescent_moon);
                        } else if (moon.equalsIgnoreCase(getString(R.string.weather_moon_last_quarter_moon))) {
                            eaBleWeatherItem.setE_moon(EABleWeather.Moon.last_quarter_moon);
                        } else if (moon.equalsIgnoreCase(getString(R.string.weather_moon_waxing_gibbous_moon))) {
                            eaBleWeatherItem.setE_moon(EABleWeather.Moon.waxing_gibbous_moon);
                        } else if (moon.equalsIgnoreCase(getString(R.string.weather_moon_waxing_crescent_moon))) {
                            eaBleWeatherItem.setE_moon(EABleWeather.Moon.waxing_crescent_moon);
                        } else if (moon.equalsIgnoreCase(getString(R.string.weather_moon_half_moon_2))) {
                            eaBleWeatherItem.setE_moon(EABleWeather.Moon.half_moon_2);
                        } else if (moon.equalsIgnoreCase(getString(R.string.weather_moon_quarter_moon))) {
                            eaBleWeatherItem.setE_moon(EABleWeather.Moon.quarter_moon);
                        }
                    }
                    String nv = activityWeatherBinding.uvIntensity.getText().toString();
                    Log.e(TAG, "紫外线强度:" + nv);
                    if (!TextUtils.isEmpty(nv)) {
                        if (nv.equalsIgnoreCase(getString(R.string.weather_rays_medium))) {
                            eaBleWeatherItem.setE_rays(EABleWeather.RaysLevel.medium);
                        } else if (nv.equalsIgnoreCase(getString(R.string.weather_rays_strong))) {
                            eaBleWeatherItem.setE_rays(EABleWeather.RaysLevel.strong);
                        } else if (nv.equalsIgnoreCase(getString(R.string.weather_rays_super_strong))) {
                            eaBleWeatherItem.setE_rays(EABleWeather.RaysLevel.super_strong);
                        } else if (nv.equalsIgnoreCase(getString(R.string.weather_rays_weak))) {
                            eaBleWeatherItem.setE_rays(EABleWeather.RaysLevel.weak);
                        } else if (nv.equalsIgnoreCase(getString(R.string.weather_rays_very_strong))) {
                            eaBleWeatherItem.setE_rays(EABleWeather.RaysLevel.very_strong);
                        }
                    }


                    eaBleWeatherItem.setMax_temperature((int) aTemp);
                    eaBleWeatherItem.setMin_temperature((int) iTemp);
                    eaBleWeatherItem.setMax_wind_power(Integer.parseInt(activityWeatherBinding.maxWindForce.getText().toString()));
                    eaBleWeatherItem.setMin_wind_power(Integer.parseInt(activityWeatherBinding.minWindForce.getText().toString()));
                    try {
                        eaBleWeatherItem.setSunrise_timestamp(simpleDateFormat.parse(activityWeatherBinding.sunriseTime.getText().toString()).getTime() / 1000);
                        eaBleWeatherItem.setSunset_timestamp(simpleDateFormat.parse(activityWeatherBinding.sunsetTime.getText().toString()).getTime() / 1000);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    weatherItemList.add(eaBleWeatherItem);
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(WeatherActivity.this);
                    }
                    if (!waitingDialog.isShowing()) {
                        waitingDialog.show();
                    }
                    Log.e(TAG, "生成的天气数据:" + JSONObject.toJSONString(eaBleWeather));
                    EABleManager.getInstance().setWeather(eaBleWeather, new GeneralCallback() {
                        @Override
                        public void result(boolean success, int reason) {
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x42);
                            }
                        }

                        @Override
                        public void mutualFail(int errorCode) {
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x43);
                            }
                        }
                    });

                }
            }
        });
    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        simpleDateFormat = null;
        if (weatherTemperatureUnitDialog != null) {
            weatherTemperatureUnitDialog.destroyDialog();
            weatherTemperatureUnitDialog = null;
        }
        if (currentWeatherTemperatureDialog != null) {
            currentWeatherTemperatureDialog.destroyDialog();
            currentWeatherTemperatureDialog = null;
        }
        if (minTempDialog != null) {
            minTempDialog.destroyDialog();
            minTempDialog = null;
        }
        if (maxTempDialog != null) {
            maxTempDialog.destroyDialog();
            maxTempDialog = null;
        }
        if (dayWeatherDialog != null) {
            dayWeatherDialog.destroyDialog();
            dayWeatherDialog = null;
        }
        if (nightWeatherDialog != null) {
            nightWeatherDialog.destroyDialog();
            nightWeatherDialog = null;
        }
        if (moonDialog != null) {
            moonDialog.destroyDialog();
            moonDialog = null;
        }
        if (uvDialog != null) {
            uvDialog.destroyDialog();
            uvDialog = null;
        }

        super.onDestroy();
    }
    /**

     class AdapterWeather extends RecyclerView.Adapter<AdapterWeather.HoldView> {
    @NonNull
    @Override public HoldView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(WeatherActivity.this).inflate(R.layout.adapter_weather_recyclerview, parent, false);
    return new HoldView(itemView);
    }

    @Override public void onBindViewHolder(@NonNull HoldView holder, int position) {
    if (eaBleWeather == null || eaBleWeather.getS_day() == null || eaBleWeather.getS_day().isEmpty()) {
    return;
    }
    EABleWeather.EABleWeatherItem weatherItem = eaBleWeather.getS_day().get(position);
    EABleWeather.WeatherType dayType = weatherItem.getE_day_type();
    if (dayType != null) {
    if (dayType == EABleWeather.WeatherType.clear) {
    holder.dayText.setText(getString(R.string.weather_type_clear));

    } else if (dayType == EABleWeather.WeatherType.cloudy) {
    holder.dayText.setText(getString(R.string.weather_type_cloudy));
    } else if (dayType == EABleWeather.WeatherType.gloomy) {
    holder.dayText.setText(getString(R.string.weather_type_gloomy));
    } else if (dayType == EABleWeather.WeatherType.drizzle) {
    holder.dayText.setText(getString(R.string.weather_type_drizzle));
    } else if (dayType == EABleWeather.WeatherType.moderate_rain) {
    holder.dayText.setText(getString(R.string.weather_type_moderate_rain));
    } else if (dayType == EABleWeather.WeatherType.thunderstorm) {
    holder.dayText.setText(getString(R.string.weather_type_thunderstorm));
    } else if (dayType == EABleWeather.WeatherType.heavy_rain) {
    holder.dayText.setText(getString(R.string.weather_type_heavy_rain));
    } else if (dayType == EABleWeather.WeatherType.sleet) {
    holder.dayText.setText(getString(R.string.weather_type_heavy_sleet));
    } else if (dayType == EABleWeather.WeatherType.light_snow) {
    holder.dayText.setText(getString(R.string.weather_type_light_snow));
    } else if (dayType == EABleWeather.WeatherType.moderate_snow) {
    holder.dayText.setText(getString(R.string.weather_type_moderate_snow));
    } else if (dayType == EABleWeather.WeatherType.heavy_snow) {
    holder.dayText.setText(getString(R.string.weather_type_heavy_snow));
    }
    }
    EABleWeather.WeatherType nightType = weatherItem.getE_night_type();
    if (nightType != null) {
    if (nightType == EABleWeather.WeatherType.clear) {
    holder.nightText.setText(getString(R.string.weather_type_clear));
    } else if (nightType == EABleWeather.WeatherType.cloudy) {
    holder.nightText.setText(getString(R.string.weather_type_cloudy));
    } else if (nightType == EABleWeather.WeatherType.gloomy) {
    holder.nightText.setText(getString(R.string.weather_type_gloomy));
    } else if (nightType == EABleWeather.WeatherType.drizzle) {
    holder.nightText.setText(getString(R.string.weather_type_drizzle));
    } else if (nightType == EABleWeather.WeatherType.moderate_rain) {
    holder.nightText.setText(getString(R.string.weather_type_moderate_rain));
    } else if (nightType == EABleWeather.WeatherType.thunderstorm) {
    holder.nightText.setText(getString(R.string.weather_type_thunderstorm));
    } else if (nightType == EABleWeather.WeatherType.heavy_rain) {
    holder.nightText.setText(getString(R.string.weather_type_heavy_rain));
    } else if (nightType == EABleWeather.WeatherType.sleet) {
    holder.nightText.setText(getString(R.string.weather_type_heavy_sleet));
    } else if (nightType == EABleWeather.WeatherType.light_snow) {
    holder.nightText.setText(getString(R.string.weather_type_light_snow));
    } else if (nightType == EABleWeather.WeatherType.moderate_snow) {
    holder.nightText.setText(getString(R.string.weather_type_moderate_snow));
    } else if (nightType == EABleWeather.WeatherType.heavy_snow) {
    holder.nightText.setText(getString(R.string.weather_type_heavy_snow));
    }
    }
    EABleWeather.AirQuality airQuality = weatherItem.getE_air();
    if (airQuality != null) {
    if (airQuality == EABleWeather.AirQuality.excellent) {
    holder.qualityText.setText(getString(R.string.weather_air_excellent));
    } else if (airQuality == EABleWeather.AirQuality.good) {
    holder.qualityText.setText(getString(R.string.weather_air_good));
    } else if (airQuality == EABleWeather.AirQuality.bad) {
    holder.qualityText.setText(getString(R.string.weather_air_bad));
    }
    }
    EABleWeather.RaysLevel raysLevel = weatherItem.getE_rays();
    if (raysLevel != null) {
    if (raysLevel == EABleWeather.RaysLevel.weak) {
    holder.uvText.setText(getString(R.string.weather_rays_weak));
    } else if (raysLevel == EABleWeather.RaysLevel.medium) {
    holder.uvText.setText(getString(R.string.weather_rays_medium));
    } else if (raysLevel == EABleWeather.RaysLevel.strong) {
    holder.uvText.setText(getString(R.string.weather_rays_strong));
    } else if (raysLevel == EABleWeather.RaysLevel.very_strong) {
    holder.uvText.setText(getString(R.string.weather_rays_very_strong));
    } else if (raysLevel == EABleWeather.RaysLevel.super_strong) {
    holder.uvText.setText(getString(R.string.weather_rays_super_strong));
    }
    }
    EABleWeather.Moon moon = weatherItem.getE_moon();
    if (moon != null) {
    if (moon == EABleWeather.Moon.new_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_new_moon));
    } else if (moon == EABleWeather.Moon.waxing_crescent_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_waxing_crescent_moon));
    } else if (moon == EABleWeather.Moon.quarter_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_quarter_moon));
    } else if (moon == EABleWeather.Moon.half_moon_1) {
    holder.moonText.setText(getString(R.string.weather_moon_half_moon_1));
    } else if (moon == EABleWeather.Moon.waxing_gibbous_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_waxing_gibbous_moon));
    } else if (moon == EABleWeather.Moon.full_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_full_moon));
    } else if (moon == EABleWeather.Moon.waning_gibbous_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_waning_gibbous_moon));
    } else if (moon == EABleWeather.Moon.half_moon_2) {
    holder.moonText.setText(getString(R.string.weather_moon_half_moon_2));
    } else if (moon == EABleWeather.Moon.last_quarter_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_last_quarter_moon));
    } else if (moon == EABleWeather.Moon.waning_crescent_moon) {
    holder.moonText.setText(getString(R.string.weather_moon_waning_crescent_moon));
    }
    }
    holder.minTempText.setText(weatherItem.getMin_temperature() + "");
    holder.maxTempText.setText(weatherItem.getMax_temperature() + "");
    calendar = Calendar.getInstance();
    calendar.setTimeInMillis(weatherItem.getSunrise_timestamp() * 1000);
    holder.sunriseText.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.YEAR) + " " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE));
    calendar.clear();
    calendar.setTimeInMillis(weatherItem.getSunset_timestamp() * 1000);
    Log.e(TAG, "日落时间:" + weatherItem.getSunrise_timestamp() * 1000);
    holder.sunsetText.setText(calendar.get(Calendar.DAY_OF_MONTH) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.YEAR) + " " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE));
    holder.minWindText.setText(weatherItem.getMin_wind_power() + "");
    holder.maxWindText.setText(weatherItem.getMax_wind_power() + "");
    holder.humidityText.setText(weatherItem.getAir_humidity() + "");
    holder.cloudinessText.setText(weatherItem.getCloudiness() + "");


    }

    @Override public int getItemCount() {
    return eaBleWeather == null ? 0 : (eaBleWeather.getS_day() == null ? 0 : eaBleWeather.getS_day().size());
    }

    class HoldView extends RecyclerView.ViewHolder {
    @BindView(R.id.daytime_weather) TextView dayText;
    @BindView(R.id.night_weather) TextView nightText;
    @BindView(R.id.min_temperature) TextView minTempText;
    @BindView(R.id.max_temperature) TextView maxTempText;
    @BindView(R.id.sunrise_time) TextView sunriseText;
    @BindView(R.id.sunset_time) TextView sunsetText;
    @BindView(R.id.air_quality) TextView qualityText;
    @BindView(R.id.min_wind_force) TextView minWindText;
    @BindView(R.id.max_wind_force) TextView maxWindText;
    @BindView(R.id.uv_intensity) TextView uvText;
    @BindView(R.id.air_humidity) TextView humidityText;
    @BindView(R.id.moon) TextView moonText;
    @BindView(R.id.cloudiness) TextView cloudinessText;


    public HoldView(@NonNull View itemView) {
    super(itemView);
    ButterKnife.bind(this, itemView);
    }
    }
    }
     */
}
